package edu.dao;

import edu.entity.Hero;
import edu.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class HeroDAO {
	private static final String SQL_ADDHERE = "INSERT INTO hero(name,hp) VALUE(?,?);";
	private static final String SQL_GETALLHERO = "SELECT * FROM hero";
	private static final String SQL_GETHERO = "SELECT * FROM hero WHERE id = ?;";
	private static final String SQL_GETHEROBYLIMIT = "SELECT * FROM hero order by id limit ?,?";
	private static final String SQL_GETTOTAL = "SELECT COUNT(*) FROM hero";

	public static void addHero(Hero here){
		try(Connection conn = DBUtil.getConnection()){
			PreparedStatement ps = conn.prepareStatement(SQL_ADDHERE);
			ps.setString(1, here.getName());
			ps.setFloat(2, here.getHp());
			ps.executeUpdate();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Hero getHero(int id){
		try(Connection conn = DBUtil.getConnection();){
			Hero hero  = new Hero();
			PreparedStatement prep = conn.prepareStatement(SQL_GETHERO);
			prep.setInt(1, id);
			ResultSet rs = prep.executeQuery();
			while(rs.next()){
				String name = rs.getString(2);
				float hp = rs.getInt(3);
				hero.setHp(hp);
				hero.setName(name);
				hero.setId(id);
				System.out.println(hero);
			}
			return hero;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static List< Hero > getHero(){
		try(Connection conn = DBUtil.getConnection();){
			List< Hero > heroList = new ArrayList< Hero >(); 
			PreparedStatement prep = conn.prepareStatement(SQL_GETALLHERO);
			ResultSet rs = prep.executeQuery();
			while(rs.next()){
				int id = rs.getInt(1);
				String name = rs.getString(2);
				float hp = rs.getFloat(3);
				Hero h = new Hero(id,name, hp);
				heroList.add(h);
			}
			return heroList;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	public static List< Hero > getHero(int start,int count){
		List< Hero > heroList = new ArrayList< Hero >();
		
		try(Connection conn = DBUtil.getConnection()){
			PreparedStatement prep = conn.prepareStatement(SQL_GETHEROBYLIMIT);
			prep.setInt(1, start);
			prep.setInt(2, count);
			ResultSet rs = prep.executeQuery();
			while(rs.next()){
				int id = rs.getInt(1);
				String name = rs.getString(2);
				float hp = rs.getFloat(3);
				Hero hero = new Hero(id, name, hp);
				heroList.add(hero);
			}
			
			if(heroList.size() != 0){
				return heroList;
			}else{
				return null;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取英雄的总个数
	 * @return
	 */
	public static int getTotal(){
		try(Connection conn = DBUtil.getConnection();){
			PreparedStatement prep = conn.prepareStatement(SQL_GETTOTAL);
			int amount = 0;
			ResultSet rs = prep.executeQuery();
			while(rs.next()) {
				amount = rs.getInt(1);
			}
			return  amount;
		}catch (Exception e){
			e.printStackTrace();;
			return 0;
		}
	}

	public static void main(String args[]){
		List< Hero > heroList  = getHero(1,5);
		for (Hero hero: heroList) {
			System.out.println(hero);
		}
	}
}
